﻿/***********************************************************************
 * <copyright file="SqlServerSiteDao.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Thursday, March 13, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using BusinessSecurity;
using DataObjects;
using DataObjects.ADO;
using DataSecurity.StandartInterfaces;

namespace DataSecurity.ADO.SqlServer.Dictionary
{
    /// <summary>
    /// SqlServerSite
    /// </summary>
    public class SqlServerSiteDao : ISite
    {
        /// <summary>
        /// Gets the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public Site Get(int id)
        {
            const string sql = @"uspSite_GetByID";
            object[] parms = { "@Id", id };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <returns></returns>
        public List<Site> GetList()
        {
            const string sql = @"uspSite_GetList";
            return Db.ReadList(sql, CommandType.StoredProcedure, Make);
        }

        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <param name="parentId">The parent identifier.</param>
        /// <returns></returns>
        public List<Site> GetList(int userId, int parentId)
        {
            const string sql = @"uspSite_GetByUser";
            object[] parms = { "@UserId", userId, "@ParentId", parentId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <param name="parentId">The parent identifier.</param>
        /// <returns></returns>
        public List<Site> GetListByParentId(int parentId)
        {
            const string sql = @"uspSite_GetByParentID";
            object[] parms = { "@ParentId", parentId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="currentPage">The current page.</param>
        /// <param name="totalPage">The total page.</param>
        /// <returns></returns>
        public List<Site> GetPaged(int currentPage, ref int totalPage)
        {
            const string sql = @"uspSite_GetPaging";
            object[] parms = { "@CurrentPage", currentPage, "@TotalPage", totalPage };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, out totalPage, parms);
        }

        /// <summary>
        /// Gets the advanced.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <param name="status">The status.</param>
        /// <param name="sort">The sort.</param>
        /// <param name="order">The order.</param>
        /// <param name="currentPage">The current page.</param>
        /// <param name="totalPage">The total page.</param>
        /// <returns></returns>
        public List<Site> GetAdvanced(string keyword, bool? status, string sort, string order, int currentPage, ref int totalPage)
        {
            const string sql = @"uspSite_GetSearchPaging";
            object[] parms =
                {
                    "@Keyword", keyword,
                    "@Status", status, 
                    "@Sort", sort, 
                    "@Order", order, 
                    "@CurrentPage", currentPage, 
                    "@TotalPage", totalPage
                };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, out totalPage, parms);
        }

        /// <summary>
        /// Inserts the specified Site.
        /// </summary>
        /// <param name="site">The site.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object Insert(Site site)
        {
            const string sql = @"uspSite_Insert";
            return Db.Insert(sql, CommandType.StoredProcedure, TakeInsert(site));
        }

        /// <summary>
        /// Updates the specified Site.
        /// </summary>
        /// <param name="site">The site.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object Update(Site site)
        {
            const string sql = @"uspSite_Update";
            return Db.Update(sql, CommandType.StoredProcedure, TakeUpdate(site));
        }

        /// <summary>
        /// Deletes the specified Site.
        /// </summary>
        /// <param name="idList">The identifier list.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public object Delete(string idList)
        {
            const string sql = @"uspSite_Delete";
            object[] parms = { "@IdList", idList };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }
        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, Site> Make = reader =>
          new Site
          {
              Id = reader["Id"].AsInt(),
              Title = reader["Title"].AsString(),
              Description = reader["Description"].AsString(),
              Controller = reader["Controller"].AsString(),
              Order = reader["Order"].AsInt(),
              ParentId = reader["ParentId"].AsInt(),
              Status = reader["Status"].AsBool(),
              IsGroup = reader["IsGroup"].AsBool()
          };

        /// <summary>
        /// Takes the insert.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        private static object[] TakeInsert(Site item)
        {
            return new object[]  
            {                
                "@Title" , item.Title,
                "@Description" , item.Description,
                "@Controller" , item.Controller,
                "@Order" , item.Order,
                "@ParentId" , item.ParentId,
                "@Status" ,item.Status,
                "@IsGroup" , item.IsGroup
            };
        }

        /// <summary>
        /// Creates query parameters list from UserGroup object
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns>
        /// Name value parameter list.
        /// </returns>
        private static object[] TakeUpdate(Site item)
        {
            return new object[]  
            {
                "@Id" , item.Id,
                "@Title" , item.Title,
                "@Description" , item.Description,
                "@Controller" , item.Controller,
                "@Order" , item.Order,
                "@ParentId" , item.ParentId,
                "@Status" ,item.Status,
                "@IsGroup" , item.IsGroup
            };
        }
    }
}
